
close all;
BASEDIR = '\\10.12.134.60\linxj\Documents\data\ ';
FRAME_DATA_PATH = fullfile(BASEDIR, 'frame_data\ ');
SCRIPT_PATH = fullfile(BASEDIR, 'sleepstg\script\ ');
% BASEDIR = '/home/linxj/Documents/data/';
% FRAME_DATA_PATH = fullfile(BASEDIR, 'frame_data/');
% SCRIPT_PATH = fullfile(BASEDIR, 'sleepstg/script/');
TYPE_NUM = 5;
R_T = 1000;
TOTAL_FEATURE_NUM = 38;
start_feature_NUM = 1;
end_FEATUURE_NUM = 29;
% train_wake_num  = 100;
% train_n1_num = 100;
% train_n2_num = 200;
% train_n3_n4_num = 100;
% train_rem_num = 100;
% test_wake_num = 100 ;%100;
% test_n1_num = 30; %30;
% test_n2_num = 150; %150;
% test_n3_n4_num = 100; %100;
% test_rem_num = 50;% 50;

train_wake_num  =500;
train_n1_num = 300;
train_n2_num = 1000;
train_n3_n4_num = 400;
train_rem_num = 600;
test_wake_num = 500 ;%100;
test_n1_num = 100; %30;
test_n2_num = 800; %150;
test_n3_n4_num = 200; %100;
test_rem_num = 200;% 50;
accurancy_array = [];
no_accurancy_array = [];
no_class = [];
aa_class = [];
DAGSVM2_acc = [];
DAGSVM2_class_acc = [];
% load data
wake_feature = load('scale_wake', '-ascii');
n1_feature = load('scale_n1', '-ascii');
n2_feature = load('scale_n2', '-ascii');
n3_n4_feature = load('scale_n3', '-ascii');
r_feature = load('scale_r', '-ascii');

wake_feature = wake_feature(randperm(length(wake_feature)), 1:1:TOTAL_FEATURE_NUM);
n1_feature = n1_feature(randperm(length(n1_feature)), 1:1:TOTAL_FEATURE_NUM);
n2_feature = n2_feature(randperm(length(n2_feature)), 1:1:TOTAL_FEATURE_NUM);
n3_n4_feature = n3_n4_feature(randperm(length(n3_n4_feature)), 1:1:TOTAL_FEATURE_NUM);
r_feature = r_feature(randperm(length(r_feature)), 1:1:TOTAL_FEATURE_NUM);
% wake_feature = load('wake.test.txt', '-ascii');
% n1_feature = load('n1.test.txt', '-ascii');
% n2_feature = load('n2.test.txt', '-ascii');
% n3_n4_feature = load('n3_n4.test.txt', '-ascii');
% r_feature = load('r.test.txt', '-ascii');

for run_time = [1 : 1: R_T]
% wake_feature = load('scale_wake', '-ascii');
% n1_feature = load('scale_n1', '-ascii');
% n2_feature = load('scale_n2', '-ascii');
% n3_n4_feature = load('scale_n3', '-ascii');
% r_feature = load('scale_r', '-ascii');

wake_feature = load('scale_wake', '-ascii');
n1_feature = load('scale_n1', '-ascii');
n2_feature = load('scale_n2', '-ascii');
n3_n4_feature = load('scale_n3', '-ascii');
r_feature = load('scale_r', '-ascii');

wake_feature = wake_feature(randperm(length(wake_feature)), 1:1:TOTAL_FEATURE_NUM);
n1_feature = n1_feature(randperm(length(n1_feature)), 1:1:TOTAL_FEATURE_NUM);
n2_feature = n2_feature(randperm(length(n2_feature)), 1:1:TOTAL_FEATURE_NUM);
n3_n4_feature = n3_n4_feature(randperm(length(n3_n4_feature)), 1:1:TOTAL_FEATURE_NUM);
r_feature = r_feature(randperm(length(r_feature)), 1:1:TOTAL_FEATURE_NUM);

% wake_feature = wake_feature(randperm(length(wake_feature)), 1:1:TOTAL_FEATURE_NUM);
% n1_feature = n1_feature(randperm(length(n1_feature)), 1:1:TOTAL_FEATURE_NUM);
% n2_feature = n2_feature(randperm(length(n2_feature)), 1:1:TOTAL_FEATURE_NUM);
% n3_n4_feature = n3_n4_feature(randperm(length(n3_n4_feature)), 1:1:TOTAL_FEATURE_NUM);
% r_feature = r_feature(randperm(length(r_feature)), 1:1:TOTAL_FEATURE_NUM);

% wake_feature = wake_feature(:,start_feature_NUM:end_FEATUURE_NUM);
% n1_feature = n1_feature(:, start_feature_NUM:end_FEATUURE_NUM);
% n2_feature = n2_feature(:, start_feature_NUM:end_FEATUURE_NUM);
% n3_n4_feature = n3_n4_feature(:, start_feature_NUM:end_FEATUURE_NUM);
% r_feature = r_feature(:,start_feature_NUM:end_FEATUURE_NUM);

% some feature
%  lists = [ 7, 12 ,13, 20, 26];
%  lists = [1,3,27,15,7,26, 4, 7,6,8,20,22,11,13,18,25];
%  wake_feature = wake_feature(:,lists);
%  n1_feature = n1_feature(:,lists );
%  n2_feature = n2_feature(:, lists);
%  n3_n4_feature = n3_n4_feature(:,lists);
%  r_feature = r_feature(:,lists);

% F_Score
% lists = [ 3    25    24    31    32    33    22    34     2     8     6    13    26    17    15     4     7    18     5    20  9    28    29    30    23];
% wake_feature = wake_feature(:,lists);
% n1_feature = n1_feature(:,lists );
% n2_feature = n2_feature(:, lists);
% n3_n4_feature = n3_n4_feature(:,lists);
% r_feature = r_feature(:,lists);

% lists = [3  1 19  25    24    22     2     8     6    13    26    17    15     4     7    18     5    20     9    28    29    30  23 ]; % [0.8025]    [0.5500]    [0.9600]    [0.9925]    [0.5300]
% wake_feature = wake_feature(:,lists);
% n1_feature = n1_feature(:,lists );
% n2_feature = n2_feature(:, lists);
% n3_n4_feature = n3_n4_feature(:,lists);
% r_feature = r_feature(:,lists);
% 
% 
% lists = [25    24     3    31    22    33    32    34     2     6     8    13    26    15    17     4     7    18     5    20  9    28    29    30  23]
% wake_feature = wake_feature(:,lists);
% n1_feature = n1_feature(:,lists );
% n2_feature = n2_feature(:, lists);
% n3_n4_feature = n3_n4_feature(:,lists);
% r_feature = r_feature(:,lists);

% LDA
% [evec,eval] = LDA(C)
% w_t = evec(:,25:29);
% wake_feature = wake_feature * w_t;
% n1_feature = n1_feature * w_t;
% n2_feature = n2_feature * w_t;
% n3_n4_feature = n3_n4_feature * w_t;
% r_feature = r_feature * w_t;

% train data 
%wake 100 n1 40 n2 100 n3andn4 100 rem 100

wake_feature_backup = wake_feature;
n1_feature_backup = n1_feature;
n2_feature_backup = n2_feature;
n3_n4_feature_backup = n3_n4_feature;
r_feature_backup = r_feature;

train_cell = {wake_feature(1:train_wake_num, :), n1_feature(1:train_n1_num, :),...
    n2_feature(1:train_n2_num, :), n3_n4_feature(1:train_n3_n4_num, :), r_feature(1:train_rem_num, :)};
train_lable = {ones(train_wake_num, 1), 2 * ones(train_n1_num, 1), ...
    3 *ones(train_n2_num, 1), 4 * ones(train_n3_n4_num, 1), 5 * ones(train_rem_num,1)};
%test data

test_cell = {n2_feature(train_n2_num + 1 : train_n2_num + test_n2_num, :),n1_feature(train_n1_num + 1 : train_n1_num + test_n1_num, :),n3_n4_feature(train_n3_n4_num + 1: train_n3_n4_num + test_n3_n4_num, :),wake_feature(train_wake_num + 1: train_wake_num + test_wake_num, :),r_feature(train_rem_num + 1 : train_rem_num + test_rem_num, :)};
test_lable = {3 * ones(test_n2_num, 1), 2 * ones(test_n1_num, 1), 4 * ones(test_n3_n4_num, 1), 1 * ones(test_wake_num, 1), 5 * ones(test_rem_num,1)};   
% call DAGSVM
[predict_lable, no_accurancy, no_class_accurancy_rate] = DAGSVM(train_lable, train_cell, test_lable, test_cell, TYPE_NUM,1);



% 23    18    19     9     1    29    24    38    22     8    30    25    37     6     7     3    21    12  5    15    31    36    26     2    11    17

%lists = [25    24     3    31    22    33    32    34     2     6     8    13    26    15    17     7     4    18     5    20   9    28    29    30    23];
% lists = [23    18    31    36    26     2    11    17 ]
% lists = [ 23    18    19     9     1    29    24    38    22     8    30    25    37    31    36    26     2    11 17 3 4 5]
lists = [ 23    18    19     9     1    29    24    38    22     8    31    36    26     2    11    17]%(1.003,0.996)
%lists = [23    18    19     9     1    29    24    38    22     8    30    25    37     6     7     3    21    12  5    15    31    36    26     2    11    17]
% lists = [ 23    18    19     9     1    29    24    38    22     8    30    25    37     6     7    35 28]
wake_feature = wake_feature(:,lists);
n1_feature = n1_feature(:,lists );
n2_feature = n2_feature(:, lists);
n3_n4_feature = n3_n4_feature(:,lists);
r_feature = r_feature(:,lists);

train_cell = {wake_feature(1:train_wake_num, :), n1_feature(1:train_n1_num, :),...
    n2_feature(1:train_n2_num, :), n3_n4_feature(1:train_n3_n4_num, :), r_feature(1:train_rem_num, :)};
train_lable = {ones(train_wake_num, 1), 2 * ones(train_n1_num, 1), ...
    3 *ones(train_n2_num, 1), 4 * ones(train_n3_n4_num, 1), 5 * ones(train_rem_num,1)};
%test data
test_cell = {n2_feature(train_n2_num + 1 : train_n2_num + test_n2_num, :),n1_feature(train_n1_num + 1 : train_n1_num + test_n1_num, :),n3_n4_feature(train_n3_n4_num + 1: train_n3_n4_num + test_n3_n4_num, :),wake_feature(train_wake_num + 1: train_wake_num + test_wake_num, :),r_feature(train_rem_num + 1 : train_rem_num + test_rem_num, :)};
test_lable = {3 * ones(test_n2_num, 1), 2 * ones(test_n1_num, 1), 4 * ones(test_n3_n4_num, 1), 1 * ones(test_wake_num, 1), 5 * ones(test_rem_num,1)};   
% call DAGSVM
[predict_lable, accurancy, aa_class_accurancy_rate] = DAGSVM1(train_lable, train_cell, test_lable, test_cell, TYPE_NUM,2);

lists = [ 23    18    19     9     1    29    24    38    22     8    30    25    37     6     7    35 28]
wake_feature = wake_feature_backup(:,lists);
n1_feature = n1_feature_backup(:,lists );
n2_feature = n2_feature_backup(:, lists);
n3_n4_feature = n3_n4_feature_backup(:,lists);
r_feature = r_feature_backup(:,lists);

train_cell = {wake_feature(1:train_wake_num, :), n1_feature(1:train_n1_num, :),...
    n2_feature(1:train_n2_num, :), n3_n4_feature(1:train_n3_n4_num, :), r_feature(1:train_rem_num, :)};
train_lable = {ones(train_wake_num, 1), 2 * ones(train_n1_num, 1), ...
    3 *ones(train_n2_num, 1), 4 * ones(train_n3_n4_num, 1), 5 * ones(train_rem_num,1)};
%test data
test_cell = {n2_feature(train_n2_num + 1 : train_n2_num + test_n2_num, :),n1_feature(train_n1_num + 1 : train_n1_num + test_n1_num, :),n3_n4_feature(train_n3_n4_num + 1: train_n3_n4_num + test_n3_n4_num, :),wake_feature(train_wake_num + 1: train_wake_num + test_wake_num, :),r_feature(train_rem_num + 1 : train_rem_num + test_rem_num, :)};
test_lable = {3 * ones(test_n2_num, 1), 2 * ones(test_n1_num, 1), 4 * ones(test_n3_n4_num, 1), 1 * ones(test_wake_num, 1), 5 * ones(test_rem_num,1)};   
% call DAGSVM
[predict_lable, DAGSVM2_accurancy, DAGSVM2_aa_class_accurancy_rate] = DAGSVM2(train_lable, train_cell, test_lable, test_cell, TYPE_NUM,2);

accurancy_array = [accurancy_array accurancy];
no_accurancy_array = [no_accurancy_array no_accurancy];
no_class = [no_class;no_class_accurancy_rate];
aa_class = [aa_class; aa_class_accurancy_rate];
DAGSVM2_acc = [DAGSVM2_acc; DAGSVM2_accurancy];
DAGSVM2_class_acc = [DAGSVM2_class_acc; DAGSVM2_aa_class_accurancy_rate];
end
sum(accurancy_array)/R_T
sum(aa_class)/R_T
sum(no_accurancy_array)/R_T
sum(no_class)/R_T
sum(DAGSVM2_acc)/R_T
sum(DAGSVM2_class_acc)/R_T
% type maps
type_map = containers.Map;
type_map('w') = 1;
type_map('W') = 1;

type_map('n1') = 2;
type_map('1') = 2;

type_map('n2') = 3;
type_map('2') = 3;

type_map('n3') = 4;
type_map('3') = 4;

type_map('n4') = 4;
type_map('4') = 4;

type_map('r') = 5;
type_map('R') = 5;


% feature extract
%modle = svmtrain(type_array, feature_array,'-s 0 -c 20 -g 0.1')
%svmpredict(pre_type_array, pre_feature_array, modle)
 
 
 
 